package com.zuihai.app.mvp.ui.activity.home;

import android.support.annotation.NonNull;
import android.support.design.widget.AppBarLayout;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuInflater;

import com.blankj.utilcode.util.LogUtils;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.UiUtils;
import com.zuihai.app.R;
import com.zuihai.app.di.component.DaggerHomeComponent;
import com.zuihai.app.di.module.HomeModule;
import com.zuihai.app.mvp.contract.HomeContract;
import com.zuihai.app.mvp.model.entity.mainhome.MainFirstDataSupport;
import com.zuihai.app.mvp.model.entity.mainhome.SubclassDataSupport;
import com.zuihai.app.mvp.presenter.HomePresenter;
import com.zuihai.app.mvp.ui.activity.base.ToolBarBaseFragment;
import com.zuihai.app.mvp.ui.adapter.ViewPagerAdapter;
import com.zuihai.app.mvp.ui.widget.MyToastPK;
import com.zuihai.app.mvp.ui.widget.tabLayout.SlidingTabNetIconLayout;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * 通过Template生成对应页面的MVP和Dagger代码,请注意输入框中输入的名字必须相同
 * 由于每个项目包结构都不一定相同,所以每生成一个文件需要自己导入import包名,可以在设置中设置自动导入包名
 * 请在对应包下按以下顺序生成对应代码,Contract->Model->Presenter->Activity->Module->Component
 * 因为生成Activity时,Module和Component还没生成,但是Activity中有它们的引用,所以会报错,但是不用理会
 * 继续将Module和Component生成完后,编译一下项目再回到Activity,按提示修改一个方法名即可
 * 如果想生成Fragment的相关文件,则将上面构建顺序中的Activity换为Fragment,并将Component中inject方法的参数改为此Fragment
 */

/**
 * Created by Zc on 2017/4/25.
 */

public class HomeFragment extends ToolBarBaseFragment<HomePresenter> implements HomeContract.View {

    private boolean isFirst;
    @BindView(R.id.tab_layout)
    SlidingTabNetIconLayout mTabLayout;

    @BindView(R.id.view_pager)
    ViewPager mViewPager;

    @BindView(R.id.appBar)
    AppBarLayout mAppBarLayout;

    private ViewPagerAdapter mAdapter;
    private List<BaseFragment> mFragments = new ArrayList<>();
    private String[] mTitles;
    private ArrayList<String> mIcons = new ArrayList<>();
    private boolean is = false;
    private MainFirstDataSupport firstSupport;

    public static HomeFragment newInstance() {
        HomeFragment fragment = new HomeFragment();
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerHomeComponent
                .builder()
                .appComponent(appComponent)
                .homeModule(new HomeModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected int initContentView() {
        return R.layout.fragment_home;
    }

    @Override
    public boolean hasBack() {
        return false;
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    protected void initData() {
        super.initData();

        firstSupport = DataSupport.findFirst(MainFirstDataSupport.class);
        if (firstSupport != null) {
            //setLogo(firstSupport.getTopLogo());
            com.blankj.utilcode.util.LogUtils.e("firstSupport  !null");
        } else {
            //setLogo(R.mipmap.zhanwei);
            com.blankj.utilcode.util.LogUtils.e("firstSupport  null");
        }
        initColums();
        initPager();

        setTitle("笑嗨");


    }

    private void initColums() {
        List<SubclassDataSupport> allHomeColus = DataSupport.findAll(SubclassDataSupport.class);

        ArrayList<String> title = new ArrayList<>();
        title.add("推荐");

        mFragments.add(HomeRecommendFragment.newInstance());
        if (firstSupport != null) {
            mIcons.add(firstSupport.getHomeTjLogo());
            com.blankj.utilcode.util.LogUtils.e("firstSupport != null");
        } else {
            mIcons.add("");
            com.blankj.utilcode.util.LogUtils.e("firstSupport null");
        }

        for (int i = 0; i < allHomeColus.size(); i++) {
            LogUtils.e(" allHomeColus.get(i).getName():" + allHomeColus.get(i).getName());
            LogUtils.e(" allHomeColus.get(i).getName():" + allHomeColus.get(i).getSubitem_id());
            if (Integer.parseInt(allHomeColus.get(i).getCategory()) == 10) {
                if (allHomeColus.get(i).getLeibie() == 1) {
                    mFragments.add(ArticleFragment.newInstance(allHomeColus.get(i).getSubitem_id()));
                    mIcons.add(allHomeColus.get(i).getIcon());
                    title.add(allHomeColus.get(i).getName());
                } else if (allHomeColus.get(i).getLeibie() == 2) {
                    mFragments.add(HomePictureFragment.newInstance(allHomeColus.get(i).getSubitem_id()));
                    mIcons.add(allHomeColus.get(i).getIcon());
                    title.add(allHomeColus.get(i).getName());
                } else {
                    MyToastPK.showSuccess("该栏目暂不支持跳转", getActivity());
               /* if (allHomeColus.get(i).getName() == "推荐" || allHomeColus.get(i).getName().equals("推荐")) {
                    mIcons.add(firstSupport.getHomeTjLogo());
                    title.add(allHomeColus.get(i).getName());
                    mFragments.add(HomeRecommendFragment.newInstance());
                } else if (allHomeColus.get(i).getSubitem_id().equals("101")) {//头条
                    mFragments.add(ArticleFragment.newInstance(allHomeColus.get(i).getSubitem_id()));
                    mIcons.add(allHomeColus.get(i).getIcon());
                    title.add(allHomeColus.get(i).getName());
                } else if (allHomeColus.get(i).getSubitem_id().equals("102")) {//图片
                    mFragments.add(HomePictureFragment.newInstance(allHomeColus.get(i).getSubitem_id()));
                    mIcons.add(allHomeColus.get(i).getIcon());
                    title.add(allHomeColus.get(i).getName());
                } else if (allHomeColus.get(i).getSubitem_id().equals("103")) {//热点
                    mFragments.add(HomeHotFragment.newInstance());
                    mIcons.add(allHomeColus.get(i).getIcon());
                    title.add(allHomeColus.get(i).getName());
                } else if (allHomeColus.get(i).getSubitem_id().equals("104")) {//视频
                    mFragments.add(HomeVieoFragment.newInstance());
                    mIcons.add(allHomeColus.get(i).getIcon());
                    title.add(allHomeColus.get(i).getName());
                } else if (allHomeColus.get(i).getSubitem_id().equals("105")) {//人物
                    mFragments.add(HomePersionFragment.newInstance());
                    mIcons.add(allHomeColus.get(i).getIcon());
                    title.add(allHomeColus.get(i).getName());
                } else if (allHomeColus.get(i).getSubitem_id().equals("106")) {//故事
                    mIcons.add(allHomeColus.get(i).getIcon());
                    mFragments.add(HomeStoryFragment.newInstance());
                    title.add(allHomeColus.get(i).getName());
                } else if (allHomeColus.get(i).getSubitem_id().equals("107")) {//互助
                    mFragments.add(HomeHelpFragment.newInstance());
                    mIcons.add(allHomeColus.get(i).getIcon());
                    title.add(allHomeColus.get(i).getName());
                } else if (allHomeColus.get(i).getSubitem_id().equals("201")) {//发现
                    mFragments.add(NewFindSecretFragment.newInstance());
                    mIcons.add(allHomeColus.get(i).getIcon());
                    title.add(allHomeColus.get(i).getName());
                } else if (allHomeColus.get(i).getSubitem_id().equals("202")) {//去处
                    mFragments.add(FindQuchuFragment.newInstance());
                    //FindWhitherFragment.newInstance()
                    mIcons.add(allHomeColus.get(i).getIcon());
                    title.add(allHomeColus.get(i).getName());
                } else if (allHomeColus.get(i).getSubitem_id().equals("203")) {//好货
                    mFragments.add(FindGoodsFragment.newInstance());
                    mIcons.add(allHomeColus.get(i).getIcon());
                    title.add(allHomeColus.get(i).getName());
                } else if (allHomeColus.get(i).getSubitem_id().equals("204")) {//服务
                    mFragments.add(FindServiceFragment.newInstance());
                    mIcons.add(allHomeColus.get(i).getIcon());
                    title.add(allHomeColus.get(i).getName());
                } else if (allHomeColus.get(i).getSubitem_id().equals("205")) {//逛街
                    //LogUtils.e("title detail: 人物");
                    mFragments.add(FindShoppingFragment.newInstance());
                    mIcons.add(allHomeColus.get(i).getIcon());
                    title.add(allHomeColus.get(i).getName());
                } else if (allHomeColus.get(i).getSubitem_id().equals("206")) {//周边
                    //LogUtils.e("title detail: 故事");
                    mIcons.add(allHomeColus.get(i).getIcon());
                    mFragments.add(FindPeripheryFragment.newInstance());
                    title.add(allHomeColus.get(i).getName());
                } else if (allHomeColus.get(i).getSubitem_id().equals("207")) {//活动
                    mFragments.add(FindActivityFragment.newInstance());
                    mIcons.add(allHomeColus.get(i).getIcon());
                    title.add(allHomeColus.get(i).getName());
                }*/
                }
            }
        }
        mTitles = title.toArray(new String[0]);
    }

    private void initPager() {
        mAdapter = new ViewPagerAdapter(getChildFragmentManager(), mViewPager);
        mAdapter.bindData(mFragments, mTitles);
        mViewPager.setAdapter(mAdapter);
        mViewPager.setOffscreenPageLimit(mAdapter.getCount());
        LogUtils.e("home title:" + mTitles.length + "  icon:" + mIcons.size());
        mTabLayout.setViewPager(mViewPager, mTitles, mIcons);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        getToolbar().getMenu().clear();
        getToolbar().inflateMenu(R.menu.menu_home);
    }

    @Override
    public void setData(Object data) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        UiUtils.SnackbarText(message);
    }
}